package com.goodwilldd.base.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Entity
@Table(name = "family")
public class Family {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Version
	private long version = 0;

	@Column(name = "family_name", nullable = false)
	String familyName;
	
	@OneToMany
	@JoinColumn(name="familyId", nullable = false)
	List<Person> myFamily = new ArrayList<Person>();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public long getVersion() {
		return version;
	}

	public String getFamilyName() {
		return familyName;
	}

	public static class Builder {
		Family built;

		Builder(String familyName) {
			built = new Family();
			built.familyName = familyName;
		}

		public Family build() {
			return built;
		}
	}

	@Override
	public String toString() {
		try {
			return (new ObjectMapper()).writerWithDefaultPrettyPrinter()
					.writeValueAsString(this);
		} catch (JsonProcessingException e) {
			return null;
		}
	}

}
